module ApplicationHelper
  def getFirstEmployees
    @vwEmployee = VW_Employee.new(
      ["EmployeeNumber",
        "EmployeeType",
        "BusinessUnit",
        "Department",
        "Office",
        "Manager",
        "JobTitle",
        "Name",
        "EMail",
        "PhoneWork",
        "PhoneCell",
        "OfficeAddress",
        "City",
        "State",
        "PostalCode",
        "Country",
        "SkypeName",
        "WikiSpaceURL",
        "BlogURL",
        "CubeLocation"])
      
    return @vwEmployee
  end
    
  def getFirstRoomPlaces
    @vw_room = VW_Room.new([
        "RoomId",
        "RoomName",
        "RoomDisplayName",
        "OfficeName",
        "PhoneNumber",
        "RoomNumber",
        "BridgeNumber",
        "BridgePIN",
        "NumSeats",
        "RoomResources",
        "InternalDial"
      ])
        
    return @vw_room
  end
  
  def getMapUrl company
    map = $SYSTEM_WEB_DIR_CONFIG["GetMapUrl"]
      
    if company.class.to_s == "String"
      company = company.downcase
    end
      
    case company
    when "homeaway"
      @strMapUrl= map["HAAustinMapUrl"]
    when "13 - homeaway.com, inc."
      @strMapUrl= map["HAAustinMapUrl"]
    when "homeaway.com"
      @strMapUrl= map["HAAustinMapUrl"]
    when "homeaway inc."
      @strMapUrl= map["HAAustinMapUrl"]
    when "homeaway uk"
      @strMapUrl= map["HAUkMapUrl"]
    when "holiday-rentals.co.uk ltd"
      @strMapUrl= map["HAUkMapUrl"]
    when "holiday-rentals.co.uk"
      @strMapUrl= map["HAUkMapUrl"]
    when "52 - holiday-rentals.co.uk ltd"
      @strMapUrl= map["HAUkMapUrl"]
    when "abritel.fr"
      @strMapUrl= map["AbritelMapUrl"]
    when "64 - abritel.fr sa"
      @strMapUrl= map["AbritelMapUrl"]
    when "owners direct"
      @strMapUrl= map["ODHRMapUrl"]
    when "54 - ownersdirect holiday rentals ltd"
      @strMapUrl= map["ODHRMapUrl"]
    when "fewo-direkt.de"
      @strMapUrl= map["FewoDirektMapUrl"]
    when "53 - fewo-direkt"
      @strMapUrl= map["FewoDirektMapUrl"]
    when "vrbo"
      @strMapUrl= map["VRBOMapUrl"]
    when "15 - vrbo"
      @strMapUrl= map["VRBOMapUrl"]
    when "15 - vrbo.com, inc."
      @strMapUrl= map["VRBOMapUrl"]
    when "bestline communications"
      @strMapUrl= map["AviquaMapUrl"]
    when "bestline communications"
      @strMapUrl= map["BestlineMapUrl"]
    when "ek media"
      @strMapUrl= map["EKMediaMapUrl"]
    when "bridgepoint consulting"
      @strMapUrl= map["BridgePointMapUrl"]
    when "sectheory"
      @strMapUrl= map["SecTheoryMapUrl"]
    when "escapia.com"
      @strMapUrl= map["ClarityMapUrl"]
    when "escapia.com"
      @strMapUrl= map["EscapiaMapUrl"]
    when "cybage"
      @strMapUrl= map["CybageMapUrl"]
    when "terrabridge"
      @strMapUrl= map["TerrabridgeMapUrl"]
    when "robert half"
      @strMapUrl= map["RobertHalfMapUrl"]
    when "o2 works"
      @strMapUrl= map["O2WorksMapUrl"]
    when "kyte"
      @strMapUrl= map["KyteMapUrl"]
    when "harte hanks"
      @strMapUrl= map["HarteHanksMapUrl"]
    when "softserv" ,"softserve"
      @strMapUrl= map["SoftservMapUrl"]
    when "26 - bed&breakfast.com"
      @strMapUrl= map["BedAndBreakfastMapUrl"]
    when "65 - homelidays"
      @strMapUrl= map["HomelidaysMapUrl"]
    when "33 - aluguetemporada"
      @strMapUrl= map["AlugueTemporadaMapUrl"]
    else
      @strMapUrl= "#"
    end
      
    @strMapUrl
  end
  
  def getOfficeLayoutUrl (company, givenName)
    map = $SYSTEM_WEB_DIR_CONFIG["GetMapUrl"]
    
    if company.class.to_s == "String"
      company = company.downcase
    end
      
    case company
    when "homeaway"
      @strLayoutURL= map["HAAustinOfficeLayout"]
    when "13 - homeaway.com, inc."
      @strLayoutURL= map["HAAustinOfficeLayout"]
    when "homeaway.com"
      @strLayoutURL=map["HAAustinOfficeLayout"]
    when "homeaway inc"
      @strLayoutURL=map["HAAustinOfficeLayout"]
    when "52 - holiday-rentals.co.uk ltd"
      @strLayoutURL=map["HAUkOfficeLayout"]
    when "homeaway uk"
      @strLayoutURL=map["HAUkOfficeLayout"]
    when "homeaway.co.uk ltd"
      @strLayoutURL=map["HAUkOfficeLayout"]
    when "holiday-rentals.co.uk ltd"
      @strLayoutURL=map["HAUkOfficeLayout"]
    when "holiday-rentals.co.uk"
      @strLayoutURL=map["HAUkOfficeLayout"]
    else
      @strLayoutURL=""
    end
      
    return @strLayoutURL + "#search=" + givenName + "&zoom=150"
  end
  
  
  def getConferenceRoomUrl(company)
    map = $SYSTEM_WEB_DIR_CONFIG["GetMapUrl"]
      
    if company.class.to_s == "String"
      company = company.downcase
    end
      
    case company
    when "homeaway"
      @strConferenceRoomUrl=map["HAAustinConferenceRoomUrl"]
    when "13 - homeaway.com, inc."
      @strConferenceRoomUrl=map["HAAustinConferenceRoomUrl"]
    when "homeaway.com"
      @strConferenceRoomUrl=map["HAAustinConferenceRoomUrl"]
    when "homeaway inc."
      @strConferenceRoomUrl=map["HAAustinConferenceRoomUrl"]
    when "homeaway uk"
      @strConferenceRoomUrl=map["HAUkConferenceRoomUrl"]
    when "homeaway.co.uk ltd"
      @strConferenceRoomUrl=map["HAUkConferenceRoomUrl"]
    when "holiday-rentals.co.uk ltd"
      @strConferenceRoomUrl=map["HAUkConferenceRoomUrl"]
    when "holiday-rentals.co.uk"
      @strConferenceRoomUrl=map["HAUkConferenceRoomUrl"]
    when "52 - holiday-rentals.co.uk ltd"
      @strConferenceRoomUrl=map["HAUkConferenceRoomUrl"]
    when "abritel.fr"
      @strConferenceRoomUrl=map["AbritelConferenceRoomUrl"]
    when "64 - abritel.fr sa"
      @strConferenceRoomUrl=map["AbritelConferenceRoomUrl"]
    when "owners direct"
      @strConferenceRoomUrl=map["ODHRConferenceRoomUrl"]
    when "54 - ownersdirect holiday rentals ltd"
      @strConferenceRoomUrl=map["ODHRConferenceRoomUrl"]
    when "fewo-direkt.de"
      @strConferenceRoomUrl=map["FewoFrankfurtConferenceRoomUrl"]
    when "53 - fewo-direkt"
      @strConferenceRoomUrl=map["FewoFrankfurtConferenceRoomUrl"]
    when "vrbo"
      @strConferenceRoomUrl=map["VRBOConferenceRoomUrl"]
    when "15 - vrbo"
      @strConferenceRoomUrl=map["VRBOConferenceRoomUrl"]
    when "aviqua"
      @strConferenceRoomUrl=""
    when "bestline communications"
      @strConferenceRoomUrl=""
    when "ek media"
      @strConferenceRoomUrl=""
    when "bridgepoint consulting"
      @strConferenceRoomUrl=""
    when "sectheory"
      @strConferenceRoomUrl=""
    when "clarity systems"
      @strConferenceRoomUrl=""
    when "escapia.com"
      @strConferenceRoomUrl=""
    when "cybage"
      @strConferenceRoomUrl=""
    when "terrabridge"
      @strConferenceRoomUrl=""
    when "robert half"
      @strConferenceRoomUrl=""
    when "o2 works"
      @strConferenceRoomUrl=""
    when "kyte"
      @strConferenceRoomUrl=""
    when "harte hanks"
      @strConferenceRoomUrl=""
    when "softserv" ,"softserve"
      @strConferenceRoomUrl=""
    when "26 - bed&breakfast.com"
      @strConferenceRoomUrl=""
    when "65 - homelidays"
      @strConferenceRoomUrl= map["HomelidaysConferenceRoomUrl"]
    when "33 - aluguetemporada"
      @strConferenceRoomUrl=""
    else
      @strConferenceRoomUrl=""
    end
      
    @strConferenceRoomUrl
  end
  
  def GetHomePageUrl(company, flag)
    response_write = ""
    
    map = $SYSTEM_WEB_DIR_CONFIG["GetMapUrl"]
    if company.class.to_s == "String"
      company = company.downcase
    end
    
    case company
    when "homeaway"
      strCompanyUrl= map["HAAustinUrl"]
    when "13 - homeaway.com, inc."
      strCompanyUrl= map["HAAustinUrl"]
    when "homeaway.com"
      strCompanyUrl=map["HAAustinUrl"]
    when "homeaway inc."
      strCompanyUrl=map["HAAustinUrl"]
    when "homeaway uk"
      strCompanyUrl=map["HAUkUrl"]
    when "homeaway.co.uk ltd"
      strCompanyUrl=map["HAUkUrl"]
    when "holiday-rentals.co.uk ltd"
      strCompanyUrl=map["HAUkUrl"]
    when "holiday-rentals.co.uk"
      strCompanyUrl=map["HAUkUrl"]
    when "52 - holiday-rentals.co.uk ltd"
      strCompanyUrl=map["HAUkUrl"]
    when "abritel.fr"
      strCompanyUrl=map["AbritelUrl"]
    when "64 - abritel.fr sa"
      strCompanyUrl=map["AbritelUrl"]
    when "owners direct"
      strCompanyUrl=map["ODHRUrl"]
    when "54 - ownersdirect holiday rentals ltd"
      strCompanyUrl=map["ODHRUrl"]
    when "fewo-direkt.de"
      strCompanyUrl=map["FewoDirektUrl"]
    when "53 - fewo-direkt"
      strCompanyUrl=map["FewoDirektUrl"]
    when "vrbo"
      strCompanyUrl=map["VRBOUrl"]
    when "15 - vrbo"
      strCompanyUrl=map["VRBOUrl"]
    when "15 - vrbo.com, inc."
      strCompanyUrl=map["VRBOUrl"]
    when "aviqua"
      strCompanyUrl=map["AviquaUrl"]
    when "bestline communications"
      strCompanyUrl=map["BestlineUrl"]
    when "ek media"
      strCompanyUrl=map["EKMediaUrl"]
    when "bridgepoint consulting"
      strCompanyUrl=map["BridgePointUrl"]
    when "sectheory"
      strCompanyUrl=map["SecTheoryUrl"]
    when "clarity systems"
      strCompanyUrl=map["ClarityUrl"]
    when "escapia.com"
      strCompanyUrl=map["EscapiaUrl"]
    when "cybage"
      strCompanyUrl=map["CybageUrl"]
    when "terrabridge"
      strCompanyUrl=map["TerrabridgeUrl"]
    when "robert half"
      strCompanyUrl=map["RobertHalfUrl"]
    when "o2 works"
      strCompanyUrl=map["O2WorksUrl"]
    when "kyte"
      strCompanyUrl=map["KyteUrl"]
    when "harte hanks"
      strCompanyUrl=map["HarteHanksUrl"]
    when "softserv" ,"softserve"
      strCompanyUrl=map["SoftservUrl"]
      strMapUrl= map["SoftservMapUrl"]
      strConferenceRoomUrl=""
    when "26 - bed&breakfast.com"
      strCompanyUrl=map["BedAndBreakfastUrl"]
    when "65 - homelidays"
      strCompanyUrl=map["HomelidaysUrl"]
    when "33 - aluguetemporada"
      strCompanyUrl=map["AlugueTemporadaUrl"]
    else
      strCompanyUrl=""
      strMapUrl= ""
      strConferenceRoomUrl=""
    end
      
    if strCompanyURL.empty? and flag== "1"
      response_write += "&nbsp;<a href=\"" + strCompanyUrl + "\" target=\"\"_blank\"\" style=\"display:inline-block;border-width:0px;\">" + company + "</a>"
    end
      
    if flag.to_i==2
      if !strMapUrl.empty?
        response_write += "&nbsp;<a class=\"jqModal\" onclick=\"javascript:SetMapUrl('"  + strMapUrl + "')\" style=\"display:inline-block;border-width:0px;\" tabindex=\"-1\"><img title=\"View Company Location on Map\" src=\"System Map.png\" style=\"border-width:0px;\"></a>"
      end
      if !strConferenceRoomUrl.empty?
        response_write += "&nbsp;<a onclick=\"javascript:ShowConferenceRoom('" + strConferenceRoomUrl + "'); style=\"display:inline-block;border-width:0px;\" tabindex=\"-1\"><img title=""View Meeting Room Location"" src=""Binoculars.png"" style=\"border-width:0px;\"></a>"
      end
      response_write += "</td>"
    end
    return response_write
  end
  
  def writeRoomsCells
    response_write = ""
    rooms = getFirstRoomPlaces.find(:limit => [1, $SYSTEM_WEB_DIR_CONFIG["conf"]["roomsCount"].to_i ], :order => "RoomName")
    rooms.each do |room|
      roomId = room.RoomId.to_s
      roomName = room.RoomDisplayName.to_s
      company = room.OfficeName.to_s
          
      if !roomName.empty? or !comapny.empty?
        response_write += "<tr><td><img src=\"/images/meeting.png\" style=\"border-width:0px;\"/></td>"
        response_write += "<td class=\"roomdetails_data_font\"><a class=\"\" onclick=\"javascript:window.gbl_var.popup.ShowRoomDetails('" + roomId + "', this, event); return false;\">" + roomName + "</a></td>"
        response_write += "<td class=\"roomdetails_data_font roomfilter\" title=\"Click here to Filter\"><a href=\"javascript:void(0);\">" + company + "</a></td>"
        response_write += "<td></td>"
        response_write += "</tr>"
      end
    end
    return response_write
  end

  def to_excel record_sets, column_names
    excel = column_names.join(",")
    record_sets.each do |record_set|
      excel.concat("\n#{record_set.get_csv}")
    end
    excel
  end

  def to_vcf employee
    vcf = "BEGIN:VCARD\n"
    vcf.concat "VERSION:2.1\n"
    vcf.concat "N:#{employee[:givenName]} #{employee[:sn]}\n"
    vcf.concat "FN:#{employee[:givenName]} #{employee[:sn]}\n"
    vcf.concat "ORG:#{employee[:company]}\n"
    vcf.concat "TITLE:#{employee[:jobtitle]}\n"
    vcf.concat "TEL;WORK;VOICE:#{employee[:phonework]}\n"
    vcf.concat "TEL;HOME;VOICE:#{employee[:homephone]}\n"
    vcf.concat "TEL;CELL;VOICE:#{employee[:mobile]}\n"
    vcf.concat "ADR;WORK:;;#{employee[:streetaddress]};#{employee[:City]};#{employee[:Country]};#{employee[:postalcode]};#{employee[:co]}\n"
    vcf.concat "LABEL;WORK;ENCODING=QUOTED-PRINTABLE:#{employee[:streetaddress]}=0D=0A#{employee[:City]} & #{employee[:postalcode]}=0D=0A#{employee[:co]}\n"
    vcf.concat "URL;WORK:http://www.homeaway.com\n"
    vcf.concat "EMAIL;PREF;INTERNET:#{employee[:mail]}\n"
    vcf.concat "REV:20080521T205724Z\n"
    vcf.concat "END:VCARD"
  end

  def get_jquery_rules model_name
    jquery_rules = {}
    Kernel.const_get(model_name).validators.each do |validator|
      validator.attributes.each do |attribute|
        rule = {:type => validator.class.to_s.gsub(/ActiveModel::Validations::|Validator/, ""), :options => validator.options}
        jquery_rules[attribute] = {} if !jquery_rules.has_key?(attribute)
        jquery_rules[attribute][rule[:type]] = rule[:options]
        jquery_rules[attribute]["Presence"] = "option" if !rule[:options][:allow_nil]
      end
    end
    jquery_rules
  end

  def get_jquery_rules_string model_name, object_name
    jquery_rules = get_jquery_rules model_name
    #raise jquery_rules.inspect
    str = ""
    first_attr = true
    supported_validators = %w[Presence Length Format Numericality]
    delimiter = " , "
    jquery_rules.each do |attr, rule|
      str.concat delimiter if !first_attr
      str += "\"#{object_name}[#{attr}]\":{"
      first_rule = true
      rule.each do |type, options|
        str.concat delimiter if !first_rule && supported_validators.include?(type)
        case type
        when "Presence" then
          str.concat "required : true"
        when "Length" then
          if options.has_key?(:minimum) && options.has_key?(:maximum)
            str.concat "minlength : #{options[:minimum]}#{delimiter}maxlength : #{options[:maximum]}"
          elsif options.has_key?("minimum")
            str.concat "minlength : #{options[:minimum]}"
          elsif options.has_key?("maximum")
            str.concat "maxlength : #{options[:maximum]}"
          end
        when "Format" then
          str.concat "regex : #{options[:with].inspect}"
        when "Numericality" then
          if options[:only_integer]
            str.concat "digits : true"
          else
            str.concat "number : true"
          end
          if options.has_key?(:less_than_or_equal_to)
            str.concat delimiter + "max : #{options[:less_than_or_equal_to]}"
          elsif options.has_key?(:greater_than_or_equal_to)
            str.concat delimiter + "min : #{options[:greater_than_or_equal_to]}"
          end
        end
        first_rule = false
      end
      str.concat "}"
      first_attr = false
    end
    str
  end

  def get_jquery_validation form_name, model_name, object_name
    "<script type=\"text/javascript\">
      $(document).ready(function () {
        $.validator.addMethod(
        \"regex\",
        function(value, element, regexp) {
          var check = false;
          return this.optional(element) || regexp.test(value);
        },
        \"Please check your input.\"
      );
        $(\"#{form_name}\").validate({
          rules: {" + get_jquery_rules_string(model_name, object_name) +
          "}
        });
      });
    </script>
    "
  end
end